package com.nebula.controller.salary;

import com.alibaba.fastjson.JSON;
import com.nebula.common.Const;
import com.nebula.common.PageResult;
import com.nebula.controller.BaseController;
import com.nebula.service.ISalaryService;
import com.nebula.service.impl.SalaryServiceImpl;
import com.nebula.vo.SalaryAnalyzeDGVo;
import com.nebula.vo.UserVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "SalaryAnalyzeController", urlPatterns = "/salaryAnalyze")
public class SalaryAnalyzeController extends BaseController {

    private final ISalaryService salaryService = new SalaryServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String navId = request.getParameter("id");
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        if (salaryService.checkNavRole(user.getRoleId(), navId)) {
            return Const.FORWARD + Const.DEFAULT_URL + "salary/salary-analyze.jsp";
        } else {
            return Const.FORWARD + "role-error.jsp";
        }
    }

    public String findByUserId(HttpServletRequest request, HttpServletResponse response){
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        String size = request.getParameter("rows");
        String page = request.getParameter("page");
        if (StringUtils.isBlank(size)) {
            size = Const.SIZE;
        }
        if (StringUtils.isBlank(page)) {
            page = Const.PAGE;
        }
        PageResult<SalaryAnalyzeDGVo> pageResult = salaryService.findSalaryAnalyzeByUserIdAndPage(user.getId(), Integer.parseInt(page), Integer.parseInt(size));

        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotalCount());
        map.put("rows", pageResult.getList());
        return Const.JSON + JSON.toJSONString(map);
    }
}
